#include "StdAfx.h"
#include "UnitTest++.h"

#include "LinkedList.h"

//////////////////////////////////////////////////
//LinkedListTest
//////////////////////////////////////////////////

TEST(TestLinkedListConstructor)
  {  
  LinkedList<int>* list = new LinkedList<int>();  
  CHECK_EQUAL(list->GetSize(), 0);
  CHECK(!list->GetFirstElement());
  CHECK(!list->GetLastElement());

  int data_1 = 1;
  CHECK(!list->GetNextElement(&data_1));
  }

TEST(TestLinkedListAddData)
  {
  LinkedList<int>* list = new LinkedList<int>();

  int data_1 = 1;
  list->AddData(&data_1);
  CHECK_EQUAL(list->GetSize(), 1);
  CHECK_EQUAL(list->GetFirstElement(), &data_1);
  CHECK_EQUAL(list->GetLastElement(), &data_1);
  CHECK(!list->GetNextElement(&data_1));

  int data_2 = 2;
  list->AddData(&data_2);
  CHECK_EQUAL(list->GetSize(), 2);
  CHECK_EQUAL(list->GetFirstElement(), &data_1);
  CHECK_EQUAL(list->GetLastElement(), &data_2);
  CHECK_EQUAL(list->GetNextElement(&data_1), &data_2);
  CHECK(!list->GetNextElement(&data_2));

  int data_3 = 3;
  list->AddData(&data_3);
  CHECK_EQUAL(list->GetSize(), 3);
  CHECK_EQUAL(list->GetFirstElement(), &data_1);
  CHECK_EQUAL(list->GetLastElement(), &data_3);
  CHECK_EQUAL(list->GetNextElement(&data_1), &data_2);
  CHECK_EQUAL(list->GetNextElement(&data_2), &data_3);
  CHECK(!list->GetNextElement(&data_3));  
  }

//------------------------------------------------

TEST(TestLinkedListRemoveData)
  {
  LinkedList<int>* list = new LinkedList<int>();

  int* data_1 = new int(1);
  int* data_2 = new int(2);
  int* data_3 = new int(3);

  list->AddData(data_1);
  list->AddData(data_2);
  list->AddData(data_3);
  CHECK_EQUAL(list->GetSize(), 3);
  CHECK_EQUAL(list->GetFirstElement(), data_1);
  CHECK_EQUAL(list->GetLastElement(), data_3);
  CHECK_EQUAL(list->GetNextElement(data_1), data_2);
  CHECK_EQUAL(list->GetNextElement(data_2), data_3);
  CHECK(!list->GetNextElement(data_3));

  list->RemoveData(data_2);
  CHECK_EQUAL(list->GetSize(), 2);
  CHECK_EQUAL(list->GetFirstElement(), data_1);
  CHECK_EQUAL(list->GetLastElement(), data_3);
  CHECK_EQUAL(list->GetNextElement(data_1), data_3);
  CHECK(!list->GetNextElement(data_2));
  CHECK(!list->GetNextElement(data_3));

  list->RemoveData(data_3);
  CHECK_EQUAL(list->GetSize(), 1);
  CHECK_EQUAL(list->GetFirstElement(), data_1);
  CHECK_EQUAL(list->GetLastElement(), data_1);
  CHECK(!list->GetNextElement(data_1));
  CHECK(!list->GetNextElement(data_2));
  CHECK(!list->GetNextElement(data_3));

  int* data_4 = new int(4);

  list->AddData(data_4);
  CHECK_EQUAL(list->GetSize(), 2);
  CHECK_EQUAL(list->GetFirstElement(), data_1);
  CHECK_EQUAL(list->GetLastElement(), data_4);
  CHECK_EQUAL(list->GetNextElement(data_1), data_4);
  CHECK(!list->GetNextElement(data_2));
  CHECK(!list->GetNextElement(data_3));
  CHECK(!list->GetNextElement(data_4));
  
  list->RemoveData(data_1);
  CHECK_EQUAL(list->GetSize(), 1);
  CHECK_EQUAL(list->GetFirstElement(), data_4);
  CHECK_EQUAL(list->GetLastElement(), data_4);
  CHECK(!list->GetNextElement(data_1));
  CHECK(!list->GetNextElement(data_2));
  CHECK(!list->GetNextElement(data_3));
  CHECK(!list->GetNextElement(data_4));

  list->RemoveData(data_4);
  CHECK_EQUAL(list->GetSize(), 0);
  CHECK(!list->GetFirstElement());
  CHECK(!list->GetLastElement());
  CHECK(!list->GetNextElement(data_1));
  CHECK(!list->GetNextElement(data_2));
  CHECK(!list->GetNextElement(data_3));
  CHECK(!list->GetNextElement(data_4));
  }

//------------------------------------------------

//////////////////////////////////////////////////